package cn.jhc.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;

import cn.jhc.bean.ShopCar;
import cn.jhc.db.ShopCarDao;
import cn.jhc.utils.Constants;

/**
 * Servlet Filter implementation class ShopCarFilter
 */
@WebFilter("/ShopCar.jsp")
public class ShopCarFilter implements Filter {

    /**
     * Default constructor. 
     */
    public ShopCarFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		DataSource dataSource = (DataSource)request.getServletContext().getAttribute(Constants.H2_DATASOURCE);
		response.setContentType("appliction/json;charset=utf8");
		List<ShopCar> shopCars;
        String filterisbn = (String) ((HttpServletRequest)request).getSession().getAttribute("filterisbn");
        System.out.println(filterisbn);
		ShopCarDao shopCarDao = new ShopCarDao(dataSource);
		shopCars = shopCarDao.findAll();
		shopCars.get(0).getImg();
		shopCars.get(0).getDisount();
	    request.setAttribute("goods", shopCars);   
		request.getRequestDispatcher("ShopCar.jsp").forward(request, response);
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
